package com.zksk.admin.service.productEnum.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zksk.admin.domain.productRelease.dto.ProductReleaseDto;
import com.zksk.admin.mapper.product.ProductReleaseDao;
import com.zksk.admin.service.customer.CustomerProdReqService;
import com.zksk.admin.service.customer.CustomerService;
import com.zksk.admin.service.org.OrgProductService;
import com.zksk.admin.service.product.ProductService;
import com.zksk.admin.service.productEnum.ProductReleaseService;
import com.zksk.common.core.constant.OrgConstants;
import com.zksk.common.core.domain.R;
import com.zksk.common.security.utils.SecurityUtils;
import com.zksk.mbg.admin.domain.*;
import com.zksk.mbg.admin.mapper.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
@Slf4j
@SuppressWarnings("all")
public class ProductReleaseServiceImpl extends ServiceImpl<ProductReleaseMapper, ProductRelease> implements ProductReleaseService {

    @Autowired
    private ProductReleaseMapper productReleaseMapper;

    @Autowired
    private ProductReleaseDao productReleaseDao;

    @Autowired
    private RateMapper rateMapper;

    @Autowired
    private RReleaseRateMapper rReleaseRateMapper;
    @Autowired
    private CustomerProdReqService customerProdReqService;
    @Autowired
    private ProductService  productService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private OrgProductService orgProductService;
    @Autowired
    private OrgProductMapper orgProductMapper;
    @Autowired
    private OrgMapper orgMapper;


    public ProductRelease dealProRelease(ProductRelease pro,ProductReleaseDto dto){


        pro.setLabel(dto.getLabel());     //产品标签
        pro.setProductEnumId(dto.getProductEnumId()); //产品枚举Id
        pro.setProductEnumName(dto.getProductEnumName()); //产品名称枚举（投标、履约、保证金函等）
        ProductEnum productEnum = productService.selectProductById(dto.getProductEnumId());
        String code = productEnum.getCode();
        pro.setCode(code);  //产品枚举编码
        pro.setRefBussiness(dto.getRefBussiness());  //关联业务
        //   pro.setDesc(dto.getDesc());   //描述
        pro.setCustomerProdReqId(dto.getCustomerProdReqId()); //客户产品需求id
        Long customerId = customerProdReqService.selectById(dto.getCustomerProdReqId()).getData().getCustomerId();
        String customerName = customerService.getById(customerId).getCustomerName();
        pro.setCustomerId(customerId);      //客户表id
        pro.setCustomerName(customerName);  //客户名称
        pro.setOrgId(dto.getOrgId());       //机构id
        pro.setOrgName(dto.getOrgName());   //机构名称

        OrgProduct orgProduct = orgProductService.selectOrgByOrgId(dto.getOrgId());

        if (orgProduct == null){
            return null;
        }
        pro.setOrgProductId(orgProduct.getId()); //机构产品id
        String orgType= orgMapper.selectById(dto.getOrgId()).getOrgType();
        pro.setOrgType(orgType); //机构类型
        pro.setOnlineStatus(dto.getOnlineStatus()); //上线状态
        pro.setCreateBy(SecurityUtils.getUsername());
        int productReleaseNum =0;
        if(pro.getId()==null){
            //新增上架产品数据
             productReleaseNum = productReleaseMapper.insert(pro);
        }else{
            productReleaseNum = productReleaseMapper.updateById(pro);
        }
        if(productReleaseNum>0){
            log.info("保存上架产品数据成功");
        }else{
            log.info("保存上架产品数据失败");
        }


        List<Rate> list = dto.getRateList();
        for(Rate rate : list){
            if(rate.getOnlineRate() ==null){
                rate.setOnlineRate(rate.getCostRatio());
            }
           int num =0;
            //新增上架产品费率数据
            if (rate.getId() == null) {
                rate.setCreateBy(SecurityUtils.getUsername());
                num = rateMapper.insert(rate);
            }else {
                rate.setUpdateBy(SecurityUtils.getUsername());
                num = rateMapper.updateById(rate);
            }
            if(num>0){
                log.info("保存上架产品数据成功");
            }else{
                log.info("保存上架产品数据失败");
            }
            //新增上架产品与费率关系表数据
            RReleaseRate releaseRate = new RReleaseRate();
            releaseRate.setReleaseId(pro.getId());
            releaseRate.setRateId(rate.getId());
            releaseRate.setActive(OrgConstants.ACTIVE_AV);
            int rReleaseRateNum =  rReleaseRateMapper.insert(releaseRate);
            if(rReleaseRateNum>0){
                log.info("保存架产品与费率关系表数据成功");
            }else{
                log.info("保存架产品与费率关系表数据失败");
            }
        }
        return pro;
    }

    @Override
    public ProductRelease setProductReleaseValue(ProductReleaseDto dto) {
        ProductRelease pte = new ProductRelease();
        ProductRelease pro = productReleaseMapper.selectById(dto.getId());
        if(pro == null || pro.equals("")){
            ProductRelease productRel = new ProductRelease();
            pte =dealProRelease(productRel,dto);
        }else{
            pte = dealProRelease(pro,dto);
        }
        return  pte;
    }

    @Override
    public List<ProductRelease> updateProductReleaseValue(ProductReleaseDto dto) {
    return  null;
    }

    @Override
    public ProductRelease checkProductReleaseDetails(Long id) {

        ProductRelease productRelease =  productReleaseMapper.selectById(id);
        if (productRelease == null){
            return null;
        }
        return productRelease;
    }

    @Override
    public List<Rate> getCustomerRate(Long id) {
        List<Rate> list = productReleaseDao.getCustomerRate(id);
        return list;
    }

    @Override
    public List<Rate> selectOrgRate(Long productEnumId, Long orgId) {
        List<Rate> orglist = productReleaseDao.selectOrgRate(productEnumId,orgId);
        return orglist;
    }

    @Override
    public List<Rate> selectProductReleaseRate(Long productEnumId, Long customerId) {
        List<Rate> rate = productReleaseDao.selectCustomerRate(productEnumId,customerId);
        return rate;
    }


    @Override
    public List<Customer> selectCustomerNameList() {
        List<Customer> list = productReleaseDao.selectCustomerList();
        return list;
    }

    @Override
    public List<Org> selectOrgNameList(Long productEnumId) {
        List<Org> list = productReleaseDao.selectOrgNameList(productEnumId);
        return list;
    }

    @Override
    public int deleteProductById(ProductRelease productRelease) {
        return productReleaseMapper.deleteById(productRelease);
    }
}
